/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package z.h.w.jar.kit.method;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *
 * @author EnzoZhong
 * @date 2011.09.02
 */
public class Text {

      /**
       * 首字母大写
       *
       * @param word
       * @return
       */
      public static synchronized String firstUpper ( String word ) {
            String[] wordArray = getFirstERest ( word );
            return wordArray[0].toUpperCase () + wordArray[1];
      }

      /**
       *
       * @param word
       * @return
       */
      public static synchronized String firstLower ( String word ) {
            String[] wordArray = getFirstERest ( word );
            return wordArray[0].toLowerCase () + wordArray[1];
      }

      private static String[] getFirstERest ( String word ) {
            String first = word.substring ( 0 , 1 );
            String rest = word.substring ( 1 , word.length () );
            return new String[] { first , rest };
      }

      /**
       * 给String类型添加双引号，为了某些转换上使用
       *
       * @param str
       * @return
       */
      public static synchronized String toStr ( String str ) {
            return "\"" + str + "\"";
      }

      /**
       *
       * @param str
       * @param target
       * @return
       */
      public static synchronized Integer count ( String str , String target ) {
            Integer total = 0;
            //看看str里面是否包含target这个字符串
            while ( str.contains ( target ) ) {
                  //包含，先累计
                  total ++;
                  //然后从0到最后一个target所在的位置截取，这样来计算有多少个target
                  str = str.substring ( 0 , str.lastIndexOf ( target ) );
            }
            return total;
      }

      /**
       *
       * @param str
       * @param percent
       * @param max
       * @return
       */
      public static String cutStr ( String str , Double percent , Integer max ) {
            int end = ( int ) ( str.length () * percent );
            String tmp = str.substring ( 0 , end );
            if ( tmp.length () > max ) {
                  tmp = tmp.substring ( 0 , max );
            }
            return tmp;
      }

      /**
       *
       * @param strUrl
       * @return
       * @throws MalformedURLException
       */
      public static String url2String ( String strUrl ) throws MalformedURLException {
            char[] BUFFER = new char[ 19890310 ];
            StringBuffer sb = new StringBuffer ();
            try {
                  URL url = new URL ( strUrl );
                  URLConnection urlConnection = url.openConnection ();
                  urlConnection.connect ();
                  InputStreamReader isr;
                  BufferedReader br;
                  try ( InputStream is = urlConnection.getInputStream () ) {
                        isr = new InputStreamReader ( is , "UTF-8" );
                        br = new BufferedReader ( isr );
                        int count;
                        while ( ( count = br.read ( BUFFER ) ) != -1 ) {
                              sb.append ( BUFFER , 0 , count );
                        }
                  }
                  isr.close ();
                  br.close ();
                  BUFFER = null;
            } catch ( MalformedURLException ex ) {
                  System.err.println ( "你输入的格式一定有问题！！" + strUrl );
                  throw ex;
            } catch ( IOException ex ) {
                  System.err.println ( ex );
            }
            return sb.toString ();
      }

      public static List<String> splitString ( String string , Integer maxLen ) {
            Integer len = string.length ();
            ArrayList<String> arrayList = new ArrayList<> ( len / maxLen - 1 );
            Integer index = 0;

            while ( index * maxLen < len ) {
                  Integer s = index * maxLen;
                  Integer e = ( index + 1 ) * maxLen > len ? len : ( index + 1 ) * maxLen;
                  arrayList.add ( string.substring ( s , e ) );
                  index ++;
            }
            return arrayList;
      }

      public static List<List<String>> splitString ( String string , String split , Integer maxLen ) {
            String[] array = string.split ( split );
            List<List<String>> reList = new ArrayList<> ( 10 );
            Integer len = array.length;

            Integer s = 0;
            Integer e = 1;

            Integer index = 0;
            List<String> tmpList = new ArrayList<> ( 10 );

            if ( string.length () > maxLen ) {
                  while ( e <= len ) {
                        tmpList = array2List ( array , s , e );
                        Integer nowLen = stringListSize ( array2List ( array , s , e ) , split );

                        if ( nowLen > maxLen ) {
                              e -= 1;
                              tmpList = array2List ( array , s , e );
                              if (  ! tmpList.isEmpty () ) {
                                    reList.add ( index , tmpList );
                                    index ++;
                                    s = e;
                              }
                        } else if ( nowLen == maxLen ) {
                              if (  ! tmpList.isEmpty () ) {
                                    reList.add ( index , tmpList );
                                    index ++;
                                    s = e;
                              }
                        } else {
                        }
                        e += 2;
                  }
                  tmpList = array2List ( array , s , e );
                  reList.add ( index , tmpList );
            } else {
                  tmpList = array2List ( array , s , len );
                  reList.add ( index , tmpList );
            }

            return reList;
      }

      private static List<String> array2List ( String[] array , Integer s , Integer e ) {
            Integer len = array.length;
            s = s > len - 1 ? len - 2 : s;
            e = e > len - 1 ? len : e;
            return Arrays.asList ( array ).subList ( s , e );
      }

      public static String list2String ( List<String> tmp , String split ) {
            StringBuffer stringBuffer = new StringBuffer ();
            for ( String str : tmp ) {
                  stringBuffer.append ( str ).append ( split );
            }
            Integer spLen = split.length ();
            Integer stringBufferLen = stringBuffer.length ();
            return stringBuffer.substring ( 0 , stringBufferLen - spLen + 1 );
      }

      public static Integer stringListSize ( List<String> tmp , String split ) {
            StringBuffer stringBuffer = new StringBuffer ();
            for ( String str : tmp ) {
                  stringBuffer.append ( str ).append ( split );
            }
            return stringBuffer.length () - split.length ();
      }

      public static String string2Left ( String string , Integer cifra ) {
            return loop ( string , cifra , new Text.CharOperation () {

                  @Override
                  public char act ( char ch , Integer cifra ) {
                        return ( char ) ( ch << cifra );
                  }

            } );
      }

      public static String string2Right ( String string , Integer cifra ) {
            return loop ( string , cifra , new Text.CharOperation () {

                  @Override
                  public char act ( char ch , Integer cifra ) {
                        return ( char ) ( ch >> cifra );
                  }

            } );
      }

      public static String string2Offset ( String string , Integer cifra ) {
            return loop ( string , cifra , new Text.CharOperation () {

                  @Override
                  public char act ( char ch , Integer cifra ) {
                        return ( char ) ( ch + cifra );
                  }

            } );
      }

      public static String loop ( String string , Integer cifra , Text.CharOperation act ) {
            StringBuffer stringBuffer = new StringBuffer ();
            Integer len = string.length ();
            for ( Integer x = 0 ; x < len ; x ++ ) {
                  stringBuffer.append ( act.act ( string.charAt ( x ) , cifra ) );
            }
            return stringBuffer.toString ();
      }

      public interface CharOperation {

            char act ( char ch , Integer cifra );

      }

}
